package com.dabai.iot.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dabai.common.core.domain.R;
import com.dabai.common.mybatis.core.page.PageQuery;
import com.dabai.common.mybatis.core.page.TableDataInfo;
import com.dabai.common.satoken.utils.LoginHelper;
import com.dabai.common.web.core.BaseController;
import com.dabai.iot.core.convert.ProductConvert;
import com.dabai.iot.core.dto.ProductInput;
import com.dabai.iot.core.dto.ProductQuery;
import com.dabai.iot.core.dto.ProductUpdate;
import com.dabai.iot.core.entity.ProductEntity;
import com.dabai.iot.core.service.IProductService;
import com.dabai.iot.core.vo.ProductSelectVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 产品管理
 *
 * @author kev1n
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

    private final IProductService productService;

    /**
     * 新增产品
     */
    @PostMapping("/add")
    @Operation(summary = "新增产品")
    public R<Void> add(@Valid @RequestBody ProductInput productInput) {
        ProductEntity product = ProductConvert.INSTANCE.convert(productInput);
        product.setTenantId(LoginHelper.getTenantId());
        return toAjax(productService.addProduct(product));
    }

    /**
     * 修改产品
     */
    @PostMapping("/update")
    @Operation(summary = "修改产品")
    public R<Void> update(@Valid @RequestBody ProductUpdate productUpdate) {
        ProductEntity product = productService.getProductCacheById(productUpdate.getId());
        if (product == null) {
            return R.fail("产品不存在");
        }

        product.setProductName(productUpdate.getProductName());
        product.setProductDesc(productUpdate.getProductDesc());
        product.setConnectType(productUpdate.getConnectType());
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除产品
     */
    @PostMapping("/remove")
    @Operation(summary = "删除产品", description = "传入主键id")
    public R<Void> remove(@Parameter(description = "主键id", required = true) @RequestParam Long id) {
        return toAjax(productService.removeProduct(id));
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @Operation(summary = "获取产品详情", description = "传入主键id")
    public R<ProductEntity> detail(@RequestParam Long id) {
        return R.ok(productService.detailById(id));
    }

    /**
     * 产品列表分页
     */
    @GetMapping("/page")
    @Operation(summary = "产品列表分页", description = "传入对应查询参数")
    public TableDataInfo<ProductEntity> page(PageQuery pageQuery, ProductQuery productQuery) {
        IPage<ProductEntity> pages = productService.getProductPage(pageQuery.build(), productQuery);
        return TableDataInfo.build(pages);
    }

    /**
     * 产品下拉数据源
     */
    @GetMapping("/select")
    @Operation(summary = "产品下拉数据源", description = "用于产品选择")
    public R<List<ProductSelectVO>> select(String productName) {
        return R.ok(productService.select(productName));
    }

    @GetMapping("/tslExport")
    @Operation(summary = "导出物模型文件")
    public R<String> exportTslFile(@RequestParam Long productId) {
        ProductEntity product = productService.getProductCacheById(productId);
        if (product == null) {
            return R.ok("操作成功", "");
        }
        return R.ok("操作成功", product.getTsl());
    }

    /**
     * 查看物模型上报示例
     */
    @GetMapping("/tslExample")
    @Operation(summary = "查看物模型上报示例")
    public R<Map<String, String>> getTslExample(@RequestParam String productKey, @RequestParam String deviceSn) {
        return R.ok(productService.getTSLExample(productKey, deviceSn));
    }

}
